<?php
//キーワード検索
require_once '../db_connect.php';
	$category_sql = '
		select distinct volunteer_information_id,volunteer_name,content
		from volunteer_information Voinf left join sub_category sCate on Voinf.sub_category_id = sCate.sub_category_id
		where sCate.sub_category_name like ?
		order by Voinf.updata_date desc
		limit ?,?
	';

	$category_count_sql = '
		select distinct count(volunteer_information_id)
		from volunteer_information Voinf left join sub_category sCate on Voinf.sub_category_id = sCate.sub_category_id
		where sCate.sub_category_name like ?
	';

	$page = 0;
	$max_limit = 10;
	$start_limit = 0;
//カテゴリ検索
	if (!empty($_GET['category_word']))
	{
		$category  = "%".htmlspecialchars($_GET['category_word'],ENT_QUOTES,"UTF-8")."%";

		$sth = $db->prepare($category_count_sql);
		$sth = $db->execute($sth,$category);

		echo "<center><a>検索の結果".array_shift($sth->fetchRow(DB_FETCHMODE_ORDERED))."件見つかりました</a></center>";

		$max_limit = 10;
		$start_limit = 0;
		$set_value = array($category,$start_limit,$max_limit);

		$sth = $db->prepare($category_sql);
		$sth = $db->execute($sth,$set_value);

		while ($row =& $sth->fetchRow(DB_FETCHMODE_ASSOC))
		{
				echo "<table id='table-02'><tr><th width='120px'>ボランティア名</th><a href='../volunteer_function/display_volunteer_information.php?volunteer_information_id=".$row['volunteer_information_id']."'><td valign='middle'>".$row['volunteer_name']."</td></a></tr><tr><th>内容</th><td>".$row['content']."</td></tr></tabel>";
		}

		$sth = $db->prepare($category_count_sql);
		$sth = $db->execute($sth,$category);
		$sth = ceil(array_shift($sth->fetchRow(DB_FETCHMODE_ORDERED))/10);

		echo "<center><table><tr>";
		for ($i = 1; $i <= $sth; $i++)
		{
			echo "<td><a href='search_display_category.php?page=".$i."&page_category=".$category."'/>".$i."</td>";
		}
		echo "</tr></table></center>";
	}
	else if (!empty($_GET['page']) && !empty($_GET['page_category']))
	{
		$category = htmlspecialchars($_GET['page_category']);

		$sth = $db->prepare($category_count_sql);
		$sth = $db->execute($sth,$category);

		echo "<center><a>検索の結果".array_shift($sth->fetchRow(DB_FETCHMODE_ORDERED))."件見つかりました</a></center>";

		$start_limit = $max_limit * $_GET['page'] - 10;
		$set_value = array($category,$start_limit,$max_limit);

		$sth = $db->prepare($category_sql);
		$sth = $db->execute($sth,$set_value);

		while ($row =& $sth->fetchRow(DB_FETCHMODE_ASSOC))
		{
				echo "<table id='table-02'><tr><th width='120px'>ボランティア名</th><a href='../volunteer_function/display_volunteer_information.php?volunteer_information_id=".$row['volunteer_information_id']."'><td valign='middle'>".$row['volunteer_name']."</td></a></tr><tr><th>内容</th><td>".$row['content']."</td></tr></tabel>";
		}

		$sth = $db->prepare($category_count_sql);
		$sth = $db->execute($sth,$category);
		$sth = ceil(array_shift($sth->fetchRow(DB_FETCHMODE_ORDERED))/10);

		echo "<center><table><tr>";
		for ($i = 1; $i <= $sth; $i++)
		{
			echo "<td><a href='search_display_category.php?page=".$i."&page_category=".$category."&target=".$targetpage."'/>".$i."</td>";
		}
		echo "</tr></table></center>";
	}